<?php defined('SYSPATH') or die('No direct script access.');

class Model_Reply extends ORM
{  
    protected $_table_name = 'replies';
    protected $_reload_on_wakeup = false;
    
    public function get_replies(Model_Ticket $ticket, $count = false)
    {
        $query = $this->_get_replies_query()
                    ->where('ticket_id', '=', $ticket->id);
                    
        if ($count) {
            $result = $query->count_all();
            return $result;
        } 
        
        $replies = $query
                    ->order_by('updated', 'DESC')
                    ->find_all()
                    ->as_array();
        return $replies;        
    }
    
    public function getAttachments()
    {
        $attachments = json_decode($this->attachments, true);
        $ticket = ORM::factory('ticket', $this->ticket_id);
        foreach ($attachments as &$attachment) {
            //$tmp = explode("\\", $attachment['filePath']); 
            //if (count($tmp) < 2) {
                $tmp = explode("/", $attachment['filePath']);   
            //}
            $filename = $tmp[count($tmp)-1];
            $filename = '/media/uploads/attachments/'.$ticket->mailbox_id.'/'.$filename;
            $attachment['filename'] = $filename;
            $attachment['filesize'] = Model_Main::filesize_format(filesize($attachment['filePath'])); 
        }
        return $attachments;         
    }
    
    protected function _get_replies_query()
    {
        return ORM::factory('reply');
    }
    
    public function add_reply(Model_Ticket $ticket, $text, $client_name = null, $email = null, $date = null, $attachments = array(), $start_created = false)
    {
        $user_id = 0;
        $user = Model_Auth::instance()->get_user();
        if ($user) {
            $user_id = $user->id;   
        }
        
        $from_email = null;
        $mailbox = ORM::factory('mailbox', $ticket->mailbox_id);
        if ($mailbox) {
            $from_email = $mailbox->email;    
        }
        
        if (!$date) {
            $date = time();
        }
        
        $company = Model_Main::get_company();
        if ($company->id != $mailbox->company_id) {
            throw new HTTP_Exception_404('Такой компании нет');    
        }
        
        $text           = Security::xss_clean($text);
        $client_name    = Security::xss_clean($client_name);
        $email          = Security::xss_clean($email);
        
        $this->ticket_id    = $ticket->id;
        $this->user_id      = $user_id;
        $this->client_name  = $client_name;
        $this->email        = $email;
        $this->text         = $text;
        $this->created      = $this->updated = $date;  
        if (count($attachments)) {
            $this->attachments = json_encode($attachments);   
        }      
        $this->save();
        
        if ($user_id && $ticket->email && $from_email && !$start_created) {
            Model_Mailer::send_mail($ticket->email, $from_email, 'Re: '.$ticket->subject, $text, $attachments);
        }
        
        if (!$user_id && !$start_created) {
            if ($ticket->status != Model_Ticket::STATUS_ACTIVE) {
                $ticket->edit_ticket_status(Model_Ticket::STATUS_ACTIVE);    
            }
            if ($ticket->assigned_id) {
                $user = ORM::factory('user', $ticket->assigned_id);
                if ($user->check_notifications(Model_User::NOTIFICATION_NEW_REPLY)) {
                    Model_Mailer::new_reply_notification($this, $ticket, $user, $mailbox);        
                }
            }     
        }
        
        $ticket->update_ticket_replies_count();
        $ticket->update_ticket_updated_time();
        
        return $this;  
    }
}
